//
//  ViewController.m
//  WK_QR
//
//  Created by TOPTEAM on 2017/4/19.
//  Copyright © 2017年 TOPTEAM. All rights reserved.
//

#import "ViewController.h"
#import "WKQRTool.h"
#import "Masonry.h"
#import "CellModel.h"
#import "SectionModel.h"
#import "WKCell.h"
#import "TypeModel.h"



@interface ViewController ()
<
UITableViewDelegate,
UITableViewDataSource
>

@property (strong, nonatomic)  NSMutableArray * marr_Section;

@property (strong, nonatomic)  UITableView * tableView;

@property (strong, nonatomic)  NSMutableArray * marr_Data;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self  setProperty];
    [self  createUI];
    [self  Create_Marr_Section];
    
}



#pragma mark==================setProperty======================
-(void)setProperty{
    
    self.navigationController.navigationBarHidden=NO;
    self.view .backgroundColor = [UIColor whiteColor];
    self.title = @"个性二维码";
    _marr_Section=[[NSMutableArray alloc]init];
    
}
#pragma mark==================createUI======================
-(void)createUI{
    _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
    _tableView.backgroundColor = [UIColor clearColor];
    _tableView.showsVerticalScrollIndicator = NO;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLineEtched;
    _tableView.delegate = self;
    _tableView.dataSource = self;
    
    [self.view addSubview:_tableView];
    [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(self.view);
        make.top.mas_equalTo(self.view).offset(0);
        make.bottom.mas_equalTo(self.view.mas_bottom).offset(0);
    }];
    
}
-(void)Create_Marr_Section{
    
    
    _marr_Data = [[NSMutableArray alloc]init];
    
    
    for (int i=1;i<4;i++) {//kQRCodeSizeType 1~3
        for (int j=2;j<5;j++) {//kQRCodeDrawType  1~4
            for (int k=2;k<4;k++) {//kQRCodeGradientType  1~3

                TypeModel * model_type = [[TypeModel alloc]init];
                model_type.kQRCodeSizeType1=i;
                model_type.kQRCodeDrawType1=j;
                model_type.kQRCodeGradientType1=k;
                [_marr_Data addObject:model_type];

            }

        }
    }

    [_marr_Section removeAllObjects];
    
    NSMutableArray * marr_Cells=[[NSMutableArray alloc]init];
    
    for(int i=0 ;i<_marr_Data.count; i++){
        
        CellModel* model =[[CellModel alloc]init];
        model.reuseIdentifier = NSStringFromClass([WKCell class]);
        model.className=NSStringFromClass([WKCell class]);
        model.height = [WKCell computeHeight:nil];
        model.selectionStyle=UITableViewCellSelectionStyleNone;
        model.accessoryType=UITableViewCellAccessoryNone;
        model.userInfo=_marr_Data[i];
        [marr_Cells addObject:model];
        
        
        SectionModel *model0=[SectionModel sectionModelWithTitle:@"" cells:@[model]];
        model0.headerhHeight=5;
        model0.footerHeight=7;
        [_marr_Section addObject:model0];
        
    }
    
    
    [_tableView reloadData];
    
    
}


#pragma mark tableView 代理相关
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return _marr_Section.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    SectionModel *sm = _marr_Section[section];
    return sm.cells.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    SectionModel *sm = _marr_Section[section];
    return sm.headerhHeight;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    SectionModel *sm = _marr_Section[section];
    return sm.footerHeight;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return nil;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    SectionModel *sm = _marr_Section[indexPath.section];
    CellModel *cm = sm.cells[indexPath.row];
    
    return cm.height;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    SectionModel *sm = _marr_Section[indexPath.section];
    CellModel *cm = sm.cells[indexPath.row];
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cm.reuseIdentifier];
    if (!cell) {
        cell = [[NSClassFromString(cm.className) alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cm.reuseIdentifier];
    }
    cell.selectionStyle = cm.selectionStyle;
    
    if ([cm.className isEqualToString:NSStringFromClass([WKCell class])]) {
        WKCell *ex_cell=(WKCell *)cell;
        
        [ex_cell relayOutUIWithType:cm.userInfo];
        
    }
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
